package com.bootdo.market.controller;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.dao.CommonDao;
import com.bootdo.market.domain.PlatformAdvunionAccountDO;
import com.bootdo.market.domain.ThirdSdkAdvunionRelationshipDO;
import com.bootdo.market.service.PlatformAdvunionAccountService;
import com.bootdo.market.service.ThirdSdkAdvunionRelationshipService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 变现方SDK和广告联盟关联表
 *
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-28 10:48:37
 */

@Controller
@RequestMapping("/market/thirdSdkAdvunionRelationship")
public class ThirdSdkAdvunionRelationshipController {
    @Autowired
    private ThirdSdkAdvunionRelationshipService thirdSdkAdvunionRelationshipService;
    @Autowired
    private CommonDao commonDao;
    @Autowired
    private PlatformAdvunionAccountService platformAdvunionAccountService;

    @GetMapping()
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:thirdSdkAdvunionRelationship")
    String ThirdSdkAdvunionRelationship(Integer unionaccountid, Model model) {
        model.addAttribute("unionaccountid", unionaccountid);
        return "market/thirdSdkAdvunionRelationship/thirdSdkAdvunionRelationship";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:thirdSdkAdvunionRelationship")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ThirdSdkAdvunionRelationshipDO> thirdSdkAdvunionRelationshipList = thirdSdkAdvunionRelationshipService.list(query);
        int total = thirdSdkAdvunionRelationshipService.count(query);
        PageUtils pageUtils = new PageUtils(thirdSdkAdvunionRelationshipList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:add")
    String add(Integer unionaccountid, Model model) {
        model.addAttribute("unionaccountid", unionaccountid);
        return "market/thirdSdkAdvunionRelationship/add";
    }

    @RequestMapping(value = "/listAllthirdSdk", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String listAllthirdSdk(HttpServletRequest request, ThirdSdkAdvunionRelationshipDO thirdSdkAdvunionRelationship) {
        List<Map<String, Object>> thirdSdklist = commonDao.listAllThirdSdk();
        List<Map<String, Object>> usedthirdSdklist = commonDao.usedThirdSdkList(thirdSdkAdvunionRelationship.getUnionaccountid());
        List<Map<String, Object>> newlist = thirdSdklist.stream().filter(stringObjectMap ->
                usedthirdSdklist.stream().filter(stringObjectMap1 ->
                        stringObjectMap.get("thirdSdkId").toString().equalsIgnoreCase(stringObjectMap1.get("thirdSdkId").toString())
                ).count() == 0).collect(Collectors.toList());
        return JSON.toJSONString(newlist);
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ThirdSdkAdvunionRelationshipDO thirdSdkAdvunionRelationship = thirdSdkAdvunionRelationshipService.get(id);
        model.addAttribute("thirdSdkAdvunionRelationship", thirdSdkAdvunionRelationship);
        return "market/thirdSdkAdvunionRelationship/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:add")
    public R save(ThirdSdkAdvunionRelationshipDO thirdSdkAdvunionRelationship) {
        PlatformAdvunionAccountDO platformAdvunionAccount = platformAdvunionAccountService.get(thirdSdkAdvunionRelationship.getUnionaccountid());
        thirdSdkAdvunionRelationship.setAdvunionid(platformAdvunionAccount.getAdvunionid());
        if (thirdSdkAdvunionRelationshipService.save(thirdSdkAdvunionRelationship) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:edit")
    public R update(ThirdSdkAdvunionRelationshipDO thirdSdkAdvunionRelationship) {
        thirdSdkAdvunionRelationshipService.update(thirdSdkAdvunionRelationship);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:remove")
    public R remove(Integer id) {
        if (thirdSdkAdvunionRelationshipService.remove(id) > 0) {
            commonDao.removerelationship(id);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:thirdSdkAdvunionRelationship:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        thirdSdkAdvunionRelationshipService.batchRemove(ids);
        Arrays.stream(ids).forEach(id -> commonDao.removerelationship(id));
        return R.ok();
    }

}
